package com.chainyoung.common.http.interceptor;

import com.chainyoung.common.utils.LogUtils;
import com.chainyoung.common.utils.SPUtil;
import com.chainyoung.common.utils.StringUtil;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Interceptor;
import okhttp3.Response;

public class ReceivedCookiesInterceptor implements Interceptor {


    @Override
    public Response intercept(Chain chain) throws IOException {

        Response originalResponse = chain.proceed(chain.request());
        if (!originalResponse.headers("Set-Cookie").isEmpty()) {

            List<String> stringList = new ArrayList<>(originalResponse.headers("Set-Cookie"));
            //final StringBuffer cookieBuffer = new StringBuffer();
            //String[] split = originalResponse.headers("Set-Cookie").get(0).split(";");
            // cookieBuffer.append(split[0]).append(";");
            SPUtil.getInstance().put("cookie", StringUtil.listToString(stringList));
            LogUtils.d("cookieBuffer", StringUtil.listToString(stringList));

        }

        return originalResponse;
    }
}
